package com.industrial.app.service.impl;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;

import com.industrial.app.domain.formal.BizFocusSchedulingProductProductionFormal;
import com.industrial.app.domain.request.BizFocusSchedulingProductProductionRequest;
import com.industrial.app.mapper.BizFocusSchedulingProductProductionFormalMapper;
import com.industrial.app.mapper.BizIndustrialDataMapper;
import com.industrial.app.service.IBizFocusSchedulingProductProductionFormalService;
import com.industrial.common.core.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 重点调度产品产量正式SService业务层处理
 *
 * @author SangGuangCan
 * @date 2024-01-02
 */
@Service
public class BizFocusSchedulingProductProductionFormalServiceImpl implements IBizFocusSchedulingProductProductionFormalService
{
    @Autowired
    private BizFocusSchedulingProductProductionFormalMapper bizFocusSchedulingProductProductionFormalMapper;
    @Autowired
    private BizIndustrialDataMapper bizIndustrialDataMapper;
    /**
     * 查询重点调度产品产量正式S
     *
     * @param baseId 重点调度产品产量正式S主键
     * @return 重点调度产品产量正式S
     */
    @Override
    public BizFocusSchedulingProductProductionFormal selectBizFocusSchedulingProductProductionFormalByBaseId(String baseId)
    {
        return bizFocusSchedulingProductProductionFormalMapper.selectBizFocusSchedulingProductProductionFormalByBaseId(baseId);
    }

    /**
     * 查询重点调度产品产量正式S列表
     *
     * @param bizFocusSchedulingProductProductionFormal 重点调度产品产量正式S
     * @return 重点调度产品产量正式S
     */
    @Override
    public List<BizFocusSchedulingProductProductionFormal> selectBizFocusSchedulingProductProductionFormalList(BizFocusSchedulingProductProductionFormal bizFocusSchedulingProductProductionFormal)
    {
        return bizFocusSchedulingProductProductionFormalMapper.selectBizFocusSchedulingProductProductionFormalList(bizFocusSchedulingProductProductionFormal);
    }

    /**
     * 新增重点调度产品产量正式S
     *
     * @param bizFocusSchedulingProductProductionFormal 重点调度产品产量正式S
     * @return 结果
     */
    @Override
    public int insertBizFocusSchedulingProductProductionFormal(BizFocusSchedulingProductProductionFormal bizFocusSchedulingProductProductionFormal)
    {
        return bizFocusSchedulingProductProductionFormalMapper.insertBizFocusSchedulingProductProductionFormal(bizFocusSchedulingProductProductionFormal);
    }

    /**
     * 修改重点调度产品产量正式S
     *
     * @param bizFocusSchedulingProductProductionFormal 重点调度产品产量正式S
     * @return 结果
     */
    @Override
    public int updateBizFocusSchedulingProductProductionFormal(BizFocusSchedulingProductProductionFormal bizFocusSchedulingProductProductionFormal)
    {
        return bizFocusSchedulingProductProductionFormalMapper.updateBizFocusSchedulingProductProductionFormal(bizFocusSchedulingProductProductionFormal);
    }

    /**
     * 批量删除重点调度产品产量正式S
     *
     * @param baseIds 需要删除的重点调度产品产量正式S主键
     * @return 结果
     */
    @Override
    public int deleteBizFocusSchedulingProductProductionFormalByBaseIds(String[] baseIds)
    {
        return bizFocusSchedulingProductProductionFormalMapper.deleteBizFocusSchedulingProductProductionFormalByBaseIds(baseIds);
    }

    /**
     * 删除重点调度产品产量正式S信息
     *
     * @param baseId 重点调度产品产量正式S主键
     * @return 结果
     */
    @Override
    public int deleteBizFocusSchedulingProductProductionFormalByBaseId(String baseId)
    {
        return bizFocusSchedulingProductProductionFormalMapper.deleteBizFocusSchedulingProductProductionFormalByBaseId(baseId);
    }

    /**
     * 获取APP表格中的数据
     *
     * @param request
     * @return
     */
    @Override
    public List<BizFocusSchedulingProductProductionFormal> getAppTableData(BizFocusSchedulingProductProductionRequest request) {
        String dates = request.getDates();
        DateTime parse = DateUtil.parse(dates, "yyyy年MM月");
        request.setDates(DateUtil.format(parse,"yyyy-MM-dd"));
        List<BizFocusSchedulingProductProductionFormal> appTableData = bizFocusSchedulingProductProductionFormalMapper.getAppTableData(request);

        if(appTableData == null || appTableData.isEmpty()){
            return null;
        }
        List<String> oneLevelIdArgs = appTableData.stream().map(BizFocusSchedulingProductProductionFormal::getTypeBaseId).collect(Collectors.toList());
        List<BizFocusSchedulingProductProductionFormal> subNodeDataArgs = bizFocusSchedulingProductProductionFormalMapper.selectSubNodeData(oneLevelIdArgs);
        if(subNodeDataArgs != null && !subNodeDataArgs.isEmpty()){
            List<String> subSubNodeDataArgs = subNodeDataArgs.stream().map(BizFocusSchedulingProductProductionFormal::getTypeBaseId).collect(Collectors.toList());
            List<BizFocusSchedulingProductProductionFormal> subSubNodeDataList = bizFocusSchedulingProductProductionFormalMapper.selectSubNodeData(subSubNodeDataArgs);
            if(subSubNodeDataList != null && !subSubNodeDataList.isEmpty()){
                for(BizFocusSchedulingProductProductionFormal item : subNodeDataArgs){
                    for(BizFocusSchedulingProductProductionFormal subSubItem : subSubNodeDataList){
                        if(StringUtils.equals(subSubItem.getTypeParentId(),item.getTypeBaseId())){
                            item.getChild().add(subSubItem);
                            break;
                        }
                    }
                }
            }
            for(BizFocusSchedulingProductProductionFormal item : appTableData){
                for(BizFocusSchedulingProductProductionFormal subItem : subNodeDataArgs){
                    if(StringUtils.equals(subItem.getTypeParentId(),item.getTypeBaseId())){
                        item.getChild().add(subItem);
                        break;
                    }
                }
            }
        }
        return appTableData;
    }

    /**
     * <p>
     * 查询数据库中数据的日期(year_month字段)并进行去重复操作<br/>
     * 主要用于的是APP端左上角的日期过滤
     * </p>
     *
     * @return 日期的数组
     */
    @Override
    public List<String> getUniqueDatesFromDatabase() {
        List<String> uniqueDatesFromDatabase = bizFocusSchedulingProductProductionFormalMapper.getUniqueDatesFromDatabase();
        if(!uniqueDatesFromDatabase.isEmpty()){
            //Mysql中去重关键字和排序不能对同一字段同时使用，这里通过代码来去重
            return uniqueDatesFromDatabase.stream().distinct().collect(Collectors.toList());
        }
        return uniqueDatesFromDatabase;
    }

    /**
     * @param pId
     * @return
     */
    @Override
    public List<BizFocusSchedulingProductProductionFormal> selectChNodeByParentId(String pId) {
        return bizFocusSchedulingProductProductionFormalMapper.selectBizFocusSchedulingProductProductionByParentId(pId);
    }
}
